﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Model;
using BLL;

using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Authorization;

namespace OAElectronics.Controllers
{
    [Authorize(Policy = "开发部")]
    public class SystemController : Controller
    {
        private readonly SystemBLL systemBLL;
        public SystemController(SystemBLL systemBLL)
        {
            this.systemBLL = systemBLL;
        }
        //人员管理
        [Authorize(Roles = "部门经理,总经理")]
        public IActionResult Organization()
        {
            return View();
        }
        //人员管理显示
        [Authorize(Roles = "部门经理,总经理")]
        public IEnumerable<Personnel> SelectPersonnel([FromBody] Personnel p)
        {
            if (p.staff_NAME==null)return systemBLL.personnels();
            return systemBLL.personnels().Where(a => a.staff_NAME.Contains(p.staff_NAME));
        }
        //部门下拉框数据
        public IEnumerable<Department> Selectdepartments()
        {
            return systemBLL.Department();
        }
        //职位下拉框数据1
        public IEnumerable<Position> SelectPosition()
        {
            return systemBLL.positions();
        }
        //职位修改
        [Authorize(Roles = "总经理")]
        public int UpdatePosition([FromBody]Position p)
        {
            if (systemBLL.Update(p)!=0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        //状态下拉框数据
        public IEnumerable<Tstate> SelectState()
        {
            return systemBLL.SelStates();
        }
        //人员管理删除
        [Authorize(Roles = "部门经理,总经理")]
        public int Delete([FromBody] Personnel p)
        {
            if (systemBLL.Delete(p.staff_ID) != 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

        //人员添加
        [Authorize(Roles = "部门经理,总经理")]
        public IActionResult Add(Personnel p)
        {
            if (systemBLL.Add(p) != 0)
            {
                return Content("添加成功");
            }
            else
            {
                return Content("添加失败");
            }
        }
        //角色管理
        [Authorize(Roles = "总经理")]
        public IActionResult Role()
        {
            return View();
        }
        //角色管理
        [Authorize(Roles = "总经理")]
        public IEnumerable<Role> SelectRole([FromBody] Role r)
        {
            if (r.Role_Name != "")
            {
                return systemBLL.Roles().Where(a => a.Role_Name.Contains(r.Role_Name));
            }
            else
            {
                return systemBLL.Roles();
            }
        }
        //角色修改
        [Authorize(Roles = "总经理")]
        public int UpdateRole([FromBody]Role r)
        {
            if (systemBLL.UpdateRole(r)!=0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        //角色添加
        [Authorize(Roles = "总经理")]
        public int AddRoles([FromBody] Role r)
        {
            if (systemBLL.AddRole(r) > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        //角色管理删除
        [Authorize(Roles = "总经理")]
        public int DelRole([FromBody] Role r)
        {
            if (systemBLL.DelRole(r.Role_ID) > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        //职位管理
        [Authorize(Roles = "部门经理,总经理")]
        public IActionResult Position()
        {
            return View();
        }
        //职位管理
        [Authorize(Roles = "部门经理,总经理")]
        public IEnumerable<Position> SelPositions([FromBody] Position p)
        {
            if (p.Job_Name != null)
            {
                IEnumerable<Position> list = systemBLL.SelPositions().Where(a => a.Job_Name.Contains(p.Job_Name));
                return list;
            }
            else
            {
                return systemBLL.SelPositions();
            }
        }
        //职位删除
        [Authorize(Roles = "部门经理,总经理")]
        public int DelPosition([FromBody] Position p)
        {
            if (systemBLL.DelPosition(p.Job_ID) != 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        //部门管理
        [Authorize(Roles = "部门经理,总经理")]
        public IActionResult Department()
        {
            return View();
        }
        //部门删除
        [Authorize(Roles = "总经理")]
        public int DelDelDepartments([FromBody]Department d)
        {
            if (systemBLL.DelDepartment(d.Depar_ID)>0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        //部门管理添加
        [Authorize(Roles = "总经理")]
        public int InsDepartments([FromBody]Department d)
        {
            if (systemBLL.AddDepartments(d)>0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        //部门修改
        [Authorize(Roles = "总经理")]
        public int UpdateDepartment([FromBody]Department d)
        {
            if (systemBLL.UpdateDepartment(d)!=0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        [Authorize(Roles = "总经理")]
        public IEnumerable<Department> SelDepartments([FromBody]Department d)
        {
            if (d.Depar_NAME!="")
            {
                IEnumerable<Department> list = systemBLL.SelDepartment().Where(a => a.Depar_NAME.Contains(d.Depar_NAME));
                return list;
            }
            else
            {
                 return systemBLL.SelDepartment();
            }
        }

        //角色添加页面
        [Authorize(Roles = "总经理")]
        public IActionResult InsRoles()
        {
            return View();
        }
        //添加人员
        [Authorize(Roles = "总经理")]
        public int AddRole([FromBody] Personnel p) 
        {
            var pl = systemBLL.Personnel().Where(x => x.staff_NAME == p.staff_NAME || x.staff_CODE == p.staff_CODE || x.username == p.username).Count();
            if (pl > 0) return 0;
            else return systemBLL.Add(p);
        }

        //修改人员
        [HttpPost]
        [Authorize(Roles = "总经理")]
        public int Upp([FromBody] Personnel p)
        {
             return systemBLL.Updatep(p);
        }

        //职位添加
        [Authorize(Roles = "总经理")]
        public int InsPosition([FromBody]Position p)
        {
            if (systemBLL.AddPosition(p)!=0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
    }
}
